package com.jikangjun.disk.recycle.control;

import com.jikangjun.disk.mybatis.flexigrid.PagingEntity;
import com.jikangjun.disk.mybatis.flexigrid.TblResponseEntity;
import com.jikangjun.disk.recycle.entity.RecycleFileEntity;
import com.jikangjun.disk.recycle.entity.SearchRecycleEntity;
import com.jikangjun.disk.spring.control.BaseController;
import com.jikangjun.disk.spring.security.SecurityUtils;
import com.jikangjun.disk.spring.security.UserDiskEntity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/recycle")
public class RecycleController extends BaseController {
    @Resource
    private com.jikangjun.disk.recycle.service.RecycleService recycleService;

    /**
     * 回收站页面
     */
    @GetMapping("/recyclePage")
    public String recyclePage() {
        return "recycle/recyclePage"; // 对应 templates/recycle/recyclePage.html
    }
    /**
     * 查询回收站文件列表
     */
    @RequestMapping("/queryRecycleFiles")
    @ResponseBody
    public TblResponseEntity<RecycleFileEntity> queryRecycleFiles(SearchRecycleEntity entity, PagingEntity paging) {
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("用户未登录");  // 或返回空结果
        }
        entity.setUserId(currentUser.getUserUuid());  // 确保currentUser不为null
        return recycleService.queryRecycleFilesByPage(entity, paging);
    }

    /**
     * 恢复单个文件
     */
    @Secured("ROLE_cldk_file_recover")
    @RequestMapping("/recoverFile")
    @ResponseBody
    public String recoverFile(@RequestParam("fileId") String fileId) {
        try {
            recycleService.recoverFile(fileId);
            return successJson("recycle.recover.success");
        } catch (Exception e) {
            return failJson("recycle.recover.fail", e.getMessage());
        }
    }

    /**
     * 批量恢复文件
     */
    @Secured("ROLE_cldk_file_recover")
    @RequestMapping("/batchRecoverFiles")
    @ResponseBody
    public String batchRecoverFiles(@RequestParam("fileIds") String fileIds) {
        try {
            recycleService.batchRecoverFiles(fileIds);
            return successJson("recycle.batch.recover.success");
        } catch (Exception e) {
            return failJson("recycle.batch.recover.fail", e.getMessage());
        }
    }

    /**
     * 永久删除文件
     */
    @Secured("ROLE_cldk_file_force_del")
    @RequestMapping("/permanentDelete")
    @ResponseBody
    public String permanentDelete(@RequestParam("fileIds") String fileIds) {
        try {
            recycleService.permanentDelete(fileIds);
            return successJson("recycle.permanent.delete.success");
        } catch (Exception e) {
            return failJson("recycle.permanent.delete.fail", e.getMessage());
        }
    }

    /**
     * 清空回收站
     */
    @Secured("ROLE_cldk_file_force_del")
    @RequestMapping("/emptyRecycleBin")
    @ResponseBody
    public String emptyRecycleBin() {
        try {
            recycleService.emptyRecycleBin();
            return successJson("recycle.empty.success");
        } catch (Exception e) {
            return failJson("recycle.empty.fail", e.getMessage());
        }
    }
}
